package day14.edu;
// Created: 14/09/2025, ChangJiang Ru

public class DeadLockTest {
    public static void main(String[] args) {

        StringBuilder s1 = new StringBuilder();
        StringBuilder s2 = new StringBuilder();

        new Thread(){
            @Override
            public void run()
            {
                synchronized (s1)
                {
                    s1.append("a");
                    s2.append("1");

/*
                    System.out.println(s1);
                    System.out.println(s2);
*/
                }

                try
                {
                    Thread.sleep(1000);
                }catch(InterruptedException e)
                {
                    e.printStackTrace();
                }

                synchronized (s2)
                {
                    s2.append("b");
                    s2.append("2");

                    System.out.println(s1);
                    System.out.println(s2);
                }
            }
        }.start();

//        System.out.println(s1.toString() + s2.toString());

        new Thread(){
            @Override
            public void run()
            {
                synchronized (s2)
                {
                    s1.append("c");
                    s2.append("3");

/*
                    System.out.println(s1);
                    System.out.println(s2);
*/
                }

                try
                {
                    Thread.sleep(1000);
                }catch(InterruptedException e)
                {
                    e.printStackTrace();
                }

                synchronized (s1)
                {
                    s2.append("d");
                    s2.append("4");
                    System.out.println(s1);
                    System.out.println(s2);
                }
            }
        }.start();


    }
}
